package com.font.web.zhongyao;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.font.bean.Dili;
import com.font.bean.DiliResult;
import com.font.bean.jsonDaata;
import com.font.bean.result.Result;
import com.font.service.DiliService;
import com.font.utils.MapDistance;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (Dili)表控制层
 *
 * @author makejava
 * @since 2023-08-04 20:06:50
 */
@RestController
@RequestMapping("dili")
public class DiliController {
    /**
     * 服务对象
     */
    @Resource
    private DiliService diliService;


    @GetMapping("list")
    public Result selectAll(@RequestParam(value = "name",required = false) String name) {

        LambdaQueryWrapper<Dili> diliLambdaQueryWrapper = new LambdaQueryWrapper<Dili>();
        diliLambdaQueryWrapper.orderByAsc(Dili::getDAble).like(StringUtils.hasText(name),Dili::getDName,name);

        List<Dili> list = diliService.list(diliLambdaQueryWrapper);
        ArrayList<DiliResult> list1 = new ArrayList<>();
        Dili wo = diliService.getOne(new LambdaQueryWrapper<Dili>().eq(Dili::getDName, "我的位置"));
        for (Dili dili1 : list) {
            GlobalCoordinates source =new GlobalCoordinates(dili1.getDJing(), dili1.getDWei());
            GlobalCoordinates target =new GlobalCoordinates(wo.getDJing(),  wo.getDWei() );
            double distance = MapDistance.getDistanceMeter(source, target, Ellipsoid.Sphere);

            DiliResult diliResult = new DiliResult();
            diliResult.setList(dili1);
            diliResult.setDisdance(distance);
            list1.add(diliResult);
        }
        return Result.success(list1);
    }

    @GetMapping("list2")
    public Result selectAll2(Dili dili) {
        List<Dili> list = diliService.list();
        List<Dili> arrayList = new ArrayList<>();
        Dili dili2 = new Dili();
        Dili wo = diliService.getOne(new LambdaQueryWrapper<Dili>().eq(Dili::getDName, "我的位置"));
        for (Dili dili1 : list) {
            if (dili1.getDName().equals("我的位置")) {

            } else {
                GlobalCoordinates source =new GlobalCoordinates(dili1.getDJing(), dili1.getDWei());
                GlobalCoordinates target =new GlobalCoordinates(wo.getDJing(),  wo.getDWei() );
                double distance = MapDistance.getDistanceMeter(source, target, Ellipsoid.Sphere);


//                double distance = MapDistance.getDistance(dili1.getDJing(), dili1.getDWei(), wo.getDJing(), wo.getDWei());
                dili1.setDisdance(distance);
                arrayList.add(dili1);
            }
        }
        System.err.println(arrayList);
        return Result.success(arrayList);
    }

    @GetMapping("order")
    public Integer order(String dKey) {
        diliService.updated();
        Dili dili = new Dili();
        dili.setDKey(dKey);
        dili.setDAble(2);
        diliService.updateById(dili);
        return 1;
    }

}

